import { createRequire } from 'module'

const require = createRequire(import.meta.url)

const express = require('express')
const checkWorkRouter = express.Router()

import { mysqlObj } from "../../utils/mysql.js"

/**
 * @api {Get} /checkWork checkWork
 * @apiGroup checkWork
 * @apiParam {String} name 文章名
 * @apiParamExample {json} Request-Example
 * {
 *  "userName": "Eve"
 * }
 */
checkWorkRouter.get('/', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>考勤页面</p>")
    res.end()
})
/**
 * @api {Get} /checkWork/insert insert
 * @apiGroup checkWork
 * @apiParam {String} name 用户
 * @@apiParamExample {json} 请求例子
 * {
 *  "username": "name"
 * }
 */
checkWorkRouter.get('/insert', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>插入考勤页面</p>")

    let recordObj = req.query.recordObj
    let date = req.query.date
    recordObj = JSON.stringify(recordObj);

    mysqlObj.create().then((conn) => {
        let state = `insert into works(record,date) values('${recordObj}', '${date}')`
        conn.query(state, function (error, results, fields) {
            if (error) throw error;
            // 输出错误
        })
    })

    res.end()
})

/**
 * @api {Get} /checkWork/delete delete
 * @apiGroup checkWork
 * @apiParam {String} name 用户
 * @@apiParamExample {json} 请求例子
 * {
 *  "username": "name"
 * }
 */
checkWorkRouter.get('/delete', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>删除考勤页面</p>")
    res.end()
})

/**
 * @api {Get} /checkWork/update update
 * @apiGroup checkWork
 * @apiParam {String} name 用户
 * @@apiParamExample {json} 请求例子
 * {
 *  "username": "name"
 * }
 */
checkWorkRouter.get('/update', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>修改考勤页面</p>")
    res.end()
})

/**
 * @api {Get} /checkWork/query query
 * @apiGroup checkWork
 * @apiParam {String} name 用户
 * @@apiParamExample {json} 请求例子
 * {
 *  "username": "name"
 * }
 */
checkWorkRouter.get('/query', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>查询考勤页面</p>")
    res.end()
})


export const checkWork = checkWorkRouter